using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Resources;
namespace MicroMsg.UI.Page
{
	public class TXBlogReadDetail : BasePage
	{
		private const string TAG = "TXBlogReadDetail";
		private MMObservableCollection<TXNews, TXBlogItem> mDataList;
		private TXNews news;
		private Popup popUp;
		private string strWebContent;
		private Uri mStrBlogUri;
		private List<TloMedia> mListImg;
		private bool isLoadComplete;
		private bool hasBeenInvokLoadedComplete;
		private ScrollNewsPicListCtrl bigPicCtrl;
		private ApplicationBarMenuItem shareToBlog;
		private ApplicationBarMenuItem shareToCircle;
		private ApplicationBarIconButton nextBtn;
		private ApplicationBarIconButton prevBtn;
		private ApplicationBar mBar;
		internal Grid LayoutRoot;
		internal Grid LayoutContent;
		internal Grid webview;
		internal WebBrowser newsDetail;
		internal Canvas canvasProgressBar;
		internal PerformanceProgressBar ProgressBar;
		private bool _contentLoaded;
		public TXBlogReadDetail()
		{
			this.InitializeComponent();
			this.popUp = new Popup();
		}
		private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.news == null || string.IsNullOrEmpty(this.news.strUrl))
			{
				return;
			}
			if (this.mDataList != null && this.mDataList.get_Count() > 0)
			{
				base.add_BackKeyPress(new EventHandler<CancelEventArgs>(this.NewsDetailInfoView_BackKeyPress));
				this.newsDetail.add_MouseEnter(new MouseEventHandler(this.newsDetail_MouseEnter));
				this.isLoadComplete = false;
				this.strWebContent = "";
				Uri uri = new Uri(this.news.strUrl);
				this.newsDetail.Navigate(uri);
				this.mStrBlogUri = uri;
				if (!string.IsNullOrEmpty(this.news.strCoverImgUrl))
				{
					SnsMediaMgr.getImg(new snsMediaDownloadInfo
					{
						string_0 = this.news.strCoverImgUrl,
						priority = SnsPriority.HIGH,
						urlType = SnsUrlType.MMSNS_URL_CDN
					}, true);
				}
				return;
			}
		}
		private void web_loadedComplete(object sender, NavigationEventArgs e)
		{
			this.isLoadComplete = true;
			if (this.ProgressBar.IsIndeterminate)
			{
				this.ProgressBar.IsIndeterminate = false;
			}
		}
		private void web_navigated(object sender, NavigationEventArgs e)
		{
			if (this.mStrBlogUri == e.get_Uri())
			{
				this.mBar.set_IsVisible(true);
				this.ProgressBar.IsIndeterminate = true;
				return;
			}
			this.mBar.set_IsVisible(false);
		}
		private void newsDetail_MouseEnter(object sender, MouseEventArgs e)
		{
			if (this.isLoadComplete && string.IsNullOrEmpty(this.strWebContent))
			{
				this.doloadedComplete();
			}
		}
		private void doloadedComplete()
		{
			if (this.hasBeenInvokLoadedComplete)
			{
				return;
			}
			try
			{
				if (string.IsNullOrEmpty(this.strWebContent))
				{
					this.strWebContent = this.newsDetail.SaveToString();
				}
				this.newsDetail.InvokeScript("eval", new string[]
				{
					"\r\n                window.onLinkPressed = function() {\r\n                    var elem = event.srcElement;\r\n                    if ( elem != null ) {\r\n                        window.external.notify(elem.getAttribute('link'));\r\n                    }\r\n                    return false;\r\n                }\r\n                window.BindLinks = function() {\r\n                    var elems = document.getElementsByTagName('img');\r\n                    for (var i = 0; i < elems.length; i++) {\r\n                        var elem = elems[i];\r\n                        var link = elem.getAttribute('src');\r\n                        elem.setAttribute('link', link);\r\n\r\n                        elem.attachEvent('onmousedown', onLinkPressed);\r\n                        \r\n                    }\r\n                }\r\n\r\n                window.onSharePressed = function() {\r\n                    var elem = event.srcElement;\r\n                    if ( elem != null ) {\r\n                        window.external.notify('blog');\r\n                    }\r\n                    return false;\r\n                }\r\n\r\n                window.onTimelinePressed = function() {\r\n                    var elem = event.srcElement;\r\n                    if ( elem != null ) {\r\n                        window.external.notify('timeline');\r\n                    }\r\n                    return false;\r\n                }\r\n                window.BindShare = function() {\r\n                    var elems = document.getElementById('share');\r\n                    if ( elems != null ) \r\n                        elems.attachEvent('onmousedown', onSharePressed);\r\n\r\n                    var elem = document.getElementById('tl');\r\n                    if ( elems != null )\r\n                        elem.attachEvent('onmousedown', onTimelinePressed);\r\n\r\n                }"
				});
				this.newsDetail.InvokeScript("BindLinks");
				this.newsDetail.InvokeScript("BindShare");
				this.hasBeenInvokLoadedComplete = true;
			}
			catch (Exception ex)
			{
				Log.smethod_1("TXBlogReadDetail", "exec js failed! e =" + ex.get_Message());
			}
		}
		private void web_scriptNotify(object sender, NotifyEventArgs e)
		{
			string text = e.get_Value().ToString();
			if (Regex.IsMatch(text, "http://[^\\[^>]*?(gif|jpg|png|jpeg|bmp|bmp)"))
			{
				new BitmapImage(new Uri(text, 1));
				this.getImgList();
				this.ShowBigPic(new ScrollNewsPicsCtrlArgs(null, 0, this.LayoutRoot, null, new MMUIEvent(this.scrollPicEnded))
				{
					mListSnsImg = this.mListImg
				});
				return;
			}
			if (text.Equals("blog"))
			{
				this.menuBlog_Click(null, null);
				return;
			}
			if (text.Equals("timeline"))
			{
				this.menuCircle_Click(null, null);
			}
		}
		public void ShowBigPic(ScrollNewsPicsCtrlArgs scrollNewsPicsCtrlArgs_0)
		{
			if (!ScrollNewsPicsCtrlArgs.CheckIsInfoOk(scrollNewsPicsCtrlArgs_0))
			{
				return;
			}
			this.bigPicCtrl = ScrollNewsPicListCtrl.getInstance();
			this.bigPicCtrl.SetArgs(scrollNewsPicsCtrlArgs_0);
			this.bigPicCtrl.Start();
		}
		private void NewsDetailInfoView_BackKeyPress(object sender, CancelEventArgs e)
		{
			if (this.bigPicCtrl != null && this.bigPicCtrl.mBIsHown)
			{
				this.bigPicCtrl.Hide();
				e.set_Cancel(true);
			}
		}
		private object scrollPicEnded(object obj1, object obj2)
		{
			return null;
		}
		private void SaveStringToIsoStore(string strWebContent)
		{
			IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
			if (userStoreForApplication.FileExists("web.htm"))
			{
				userStoreForApplication.DeleteFile("web.htm");
			}
			StreamResourceInfo streamResourceInfo = new StreamResourceInfo(new MemoryStream(Encoding.get_UTF8().GetBytes(strWebContent)), "html/text");
			using (BinaryReader binaryReader = new BinaryReader(streamResourceInfo.get_Stream()))
			{
				byte[] array = binaryReader.ReadBytes((int)streamResourceInfo.get_Stream().get_Length());
				using (BinaryWriter binaryWriter = new BinaryWriter(userStoreForApplication.CreateFile("web.htm")))
				{
					binaryWriter.Write(array);
					binaryWriter.Close();
				}
			}
		}
		private void btnLoad_Click(object sender, RoutedEventArgs e)
		{
			this.newsDetail.Navigate(new Uri("web.htm", 2));
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("TXBlogReadDetail", "OnPageIn start");
			this.newsDetail.set_Background(R.theme.CHAT_BK_BRUSH);
			this.webview.set_Background(R.theme.CHAT_BK_BRUSH);
			if (object_0 != null && Enumerable.Count<object>(object_0) == 2)
			{
				this.news = (object_0[0] as TXNews);
				this.mDataList = (object_0[1] as MMObservableCollection<TXNews, TXBlogItem>);
			}
			base.set_ApplicationBar(this.GetApplicationBar());
			this.PhoneApplicationPage_Loaded(null, null);
		}
		protected override void OnPageOut()
		{
		}
		public ApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				if (!this.mBar.get_IsVisible())
				{
					this.mBar.set_IsVisible(true);
				}
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Mode(0);
			this.shareToCircle = new ApplicationBarMenuItem(strings.Reader_Share_To_Album_Title);
			this.shareToCircle.add_Click(new EventHandler(this.menuCircle_Click));
			this.mBar.get_MenuItems().Add(this.shareToCircle);
			this.shareToBlog = new ApplicationBarMenuItem(strings.Reader_Share_To_Weibo_Title);
			this.shareToBlog.add_Click(new EventHandler(this.menuBlog_Click));
			this.mBar.get_MenuItems().Add(this.shareToBlog);
			this.prevBtn = new ApplicationBarIconButton(new Uri("/Resource/Img/CommonUse/ico_previousStep.png", 2));
			this.prevBtn.set_Text(strings.TXBlog_Previous);
			this.prevBtn.add_Click(new EventHandler(this.prev_Click));
			this.mBar.get_Buttons().Add(this.prevBtn);
			this.nextBtn = new ApplicationBarIconButton(new Uri("/Resource/Img/CommonUse/ico_nextStep.png", 2));
			this.nextBtn.set_Text(strings.TXBlog_Next);
			this.nextBtn.add_Click(new EventHandler(this.next_Click));
			this.mBar.get_Buttons().Add(this.nextBtn);
			this.RefreshAppBar();
			return this.mBar;
		}
		public void RefreshAppBar()
		{
			this.nextBtn.set_IsEnabled(true);
			this.prevBtn.set_IsEnabled(true);
			int num = this.mDataList.IndexOf(this.news);
			if (num == -1)
			{
				this.nextBtn.set_IsEnabled(false);
				this.prevBtn.set_IsEnabled(false);
				Log.smethod_1("TXBlogReadDetail", "error , can not find current item ");
				return;
			}
			if (num <= 1)
			{
				this.prevBtn.set_IsEnabled(false);
				return;
			}
			if (num >= this.mDataList.get_Count() - 2)
			{
				this.nextBtn.set_IsEnabled(false);
				return;
			}
			if (this.mDataList[num - 1].isCreateTime)
			{
				this.prevBtn.set_IsEnabled(false);
			}
			if (this.mDataList[num + 1].isBlankItem)
			{
				this.nextBtn.set_IsEnabled(false);
			}
		}
		private void menuBlog_Click(object sender, EventArgs e)
		{
			string reader_Share_To_Weibo_Title = strings.Reader_Share_To_Weibo_Title;
			string text = this.news.type + ";" + this.news.strShorturl;
			string text2 = string.Concat(new string[]
			{
				strings.Reader_Share_Weibo,
				" ",
				this.news.strTitle,
				" ",
				this.news.strShorturl
			});
			ToolFunction.GotoPageForUserControl("/Source/Plugin/TXBlogRead/UI/EditBlogContentPage.xaml", new object[]
			{
				SendCardType.MM_SENDCARD_READER,
				reader_Share_To_Weibo_Title,
				text,
				text2
			}, false);
		}
		private void menuCircle_Click(object sender, EventArgs e)
		{
			if (!this.isLoadComplete)
			{
				return;
			}
			if (!this.hasBeenInvokLoadedComplete && string.IsNullOrEmpty(this.strWebContent))
			{
				this.strWebContent = this.newsDetail.SaveToString();
				this.getImgList();
			}
			BitmapImage bitmapImage = ImageCacheMgr.loadFromBuf(SnsMediaStorage.load(this.news.strCoverImgUrl));
			string reader_Share_To_Album_Title = strings.Reader_Share_To_Album_Title;
			string text = this.news.strTitle + "\n" + this.news.strSource;
			ToolFunction.GotoPageForUserControl("/Source/Plugin/TXBlogRead/UI/EditCircleContentPage.xaml", new object[]
			{
				new EditCircleContentPage.DelegateComment(this.onBoxReturnHandler),
				reader_Share_To_Album_Title,
				this.news.strTitle,
				text,
				bitmapImage
			}, false);
		}
		public void onBoxReturnHandler(string strComment)
		{
			ContentObject contentObject = new ContentObject();
			contentObject.strContentUrl = this.news.strUrl;
			contentObject.strTitle = this.news.strTitle;
			contentObject.strDescription = this.news.strSource;
			contentObject.appInfo = new TloAppInfo();
			contentObject.appInfo.strAppName = strings.Contacts_Plugin_Blog;
			contentObject.appInfo.strFromUrl = this.news.strUrl;
			if (!string.IsNullOrEmpty(this.news.strCoverImgUrl))
			{
				contentObject.mediaList = new List<TloMedia>();
				TloMedia tloMedia = new TloMedia();
				tloMedia.nUrlType = 1;
				tloMedia.strUrl = this.news.strCoverImgUrl;
				tloMedia.nThumbType = 1;
				tloMedia.strThumb = this.news.strCoverImgUrl;
				tloMedia.nType = 2;
				tloMedia.nPrivate = 0;
				contentObject.mediaList.Add(tloMedia);
			}
			SnsInfo snsObj = SnsPostService.createShareSnsObject(contentObject, Util.NullAsNil(strComment), SnsObjectType.MMSNS_OBJECT_FEED);
			SnsPostService.shareToTimeLine(snsObj);
		}
		private void getImgList()
		{
			if (string.IsNullOrEmpty(this.strWebContent))
			{
				return;
			}
			string text = "data-original=\"";
			MatchCollection matchCollection = Regex.Matches(this.strWebContent, text + "http://[^\\[^>]*?(jpg|png|jpeg)");
			Dictionary<int, string> dictionary = new Dictionary<int, string>();
			IEnumerator enumerator = matchCollection.GetEnumerator();
			int num = 1;
			if (this.mListImg == null)
			{
				this.mListImg = new List<TloMedia>();
			}
			else
			{
				this.mListImg.Clear();
			}
			while (enumerator.MoveNext())
			{
				string text2 = enumerator.get_Current().ToString().Substring(text.get_Length());
				if (!dictionary.ContainsValue(text2))
				{
					TloMedia tloMedia = new TloMedia();
					tloMedia.nType = 2;
					tloMedia.strUrl = text2;
					tloMedia.nUrlType = 1;
					tloMedia.strThumb = text2;
					tloMedia.nThumbType = 1;
					tloMedia.nID = (ulong)((long)num);
					this.mListImg.Add(tloMedia);
					dictionary.Add(num, text2);
					num++;
				}
			}
		}
		private void next_Click(object sender, EventArgs e)
		{
			int num = this.mDataList.IndexOf(this.news);
			if (num == -1)
			{
				Log.smethod_1("TXBlogReadDetail", "error , can not find current item ");
				this.RefreshAppBar();
				return;
			}
			if (num >= this.mDataList.get_Count() - 2)
			{
				Log.smethod_1("TXBlogReadDetail", "error , already last one");
				this.RefreshAppBar();
				return;
			}
			TXNews tXNews = this.mDataList[num + 1];
			if (tXNews.isBlankItem)
			{
				Log.smethod_1("TXBlogReadDetail", "error , already last one");
				this.RefreshAppBar();
				return;
			}
			this.news = tXNews;
			this.PhoneApplicationPage_Loaded(null, null);
			this.RefreshAppBar();
		}
		private void prev_Click(object sender, EventArgs e)
		{
			int num = this.mDataList.IndexOf(this.news);
			if (num == -1)
			{
				Log.smethod_1("TXBlogReadDetail", "error , can not find current item ");
				this.RefreshAppBar();
				return;
			}
			if (num <= 1)
			{
				Log.smethod_1("TXBlogReadDetail", "error , already first one");
				this.RefreshAppBar();
				return;
			}
			TXNews tXNews = this.mDataList[num - 1];
			if (tXNews.isCreateTime)
			{
				Log.smethod_1("TXBlogReadDetail", "error , already first one");
				this.RefreshAppBar();
				return;
			}
			this.news = tXNews;
			this.PhoneApplicationPage_Loaded(null, null);
			this.RefreshAppBar();
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/TXBlogRead/UI/TXBlogReadDetail.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.LayoutContent = (Grid)base.FindName("LayoutContent");
			this.webview = (Grid)base.FindName("webview");
			this.newsDetail = (WebBrowser)base.FindName("newsDetail");
			this.canvasProgressBar = (Canvas)base.FindName("canvasProgressBar");
			this.ProgressBar = (PerformanceProgressBar)base.FindName("ProgressBar");
		}
	}
}
